/**
 * $Id: init-deinit.c 3 2010-10-02 23:39:41Z pmedia@xi-intersection.net $
 */

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

// Globals (e.g. system functions and globally shared definitions)
#include "include/global.h"

// Game Modules
#include "include/init-deinit.h"
#include "include/input.h"
#include "include/logic.h"
#include "include/main.h"
#include "include/render.h"

// -----------------------------------------------------------------------------
// Global Variables
// -----------------------------------------------------------------------------

struct VideoSettings videoSettings;
SDL_Surface* screen;

// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------

/**
 * Initialize the engine
 * 
 * This means:
 * - load configuration data
 * - prepare everything else needed to get into the mainloop without any trouble
 * 
 */

void init(int argc, char* argv[]) {
	int result;
	
	result = SDL_Init(SDL_INIT_VIDEO);
	if (result) {
		fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
		exit(1);
	}
	
	loadConfig(argc, argv);
	
	screen = SDL_SetVideoMode(videoSettings.hRes, videoSettings.vRes, videoSettings.depth, SDL_OPENGL | (SDL_FULLSCREEN * videoSettings.fullscreen));
	if (screen == NULL) {
		fprintf(stderr, "Unable to init videomode %ix%i: %s\n", videoSettings.hRes, videoSettings.vRes, SDL_GetError());
		exit(1);
	}
	
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glShadeModel(GL_SMOOTH);
}

/**
 * Re-Initialize the engine
 * 
 * Does nearly exactly the same than init() with one difference:
 * There will be no configuration data loaded.
 * Also, just the things that need to be redone will be redone.
 * 
 */

void reinit(void) {
}

/**
 * Cleans everything in this scope up.
 * 
 */

void deinit(void) {
	SDL_Quit();
}


/**
 * Set Settings to safe defaults
 * 
 */

void setDefaults(void) {
	videoSettings.hRes  = 640;
	videoSettings.vRes  = 480;
	videoSettings.depth = 32;
	
	videoSettings.fpsLimit   = TRUE;
	videoSettings.fullscreen = FALSE;
}

/**
 * loads the configuration data of the user
 * 
 */

void loadConfig(int argc, char *argv[]) {
	setDefaults();
	
	loadConfigFile("~/.config/games/astroid/config.cfg");
}

/**
 * Loads a specific configuration file
 */
void loadConfigFile(char *file) {
}
